package com.xbongbong.sys.model;

import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.saas.constant.ErrorMessageConstant;
import com.xbongbong.saas.domain.dao.ShareRuleDao;
import com.xbongbong.saas.domain.entity.ShareRuleEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author leex
 */
@Service("shareRuleModel")
@Slf4j
public class ShareRuleModelImpl implements ShareRuleModel {

    @Resource
    private ShareRuleDao shareRuleDao;

    @Override
    public Integer insert(ShareRuleEntity entity) {
        return shareRuleDao.insert(entity);
    }

    @Override
    public Integer update(ShareRuleEntity entity) throws XbbException {
        if (entity.getCorpid() == null) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, "corpid参数缺失！");
        }
        Integer num;
        try {
            num = shareRuleDao.update(entity);
        } catch (Exception e) {
            log.error("共享规则更新出错", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, I18nMessageUtil.getMessage(ErrorMessageConstant.SAVE_ERROR));
        }
        return num;
    }

    @Override
    public Integer deleteByKey(Long key, String corpid) {
        return shareRuleDao.deleteByKey(key, corpid);
    }

    @Override
    public Integer deleteBatch(List<Long> list, String corpid) {
        return shareRuleDao.deleteBatch(list, corpid);
    }

    @Override
    public ShareRuleEntity getByKey(Long key, String corpid) {
        return shareRuleDao.getByKey(key, corpid);
    }

    @Override
    public List<ShareRuleEntity> getByIdIn(List<Long> idIn, String corpid) {
        return shareRuleDao.getByIdIn(idIn, corpid);
    }

    @Override
    public List<ShareRuleEntity> findEntitys(Map<String, Object> param) {
        return shareRuleDao.findEntitys(param);
    }

    @Override
    public Integer getAllRuleCount(String corpid) {
        Map<String,Object> params = new HashMap<>();
        params.put("corpid",corpid);
        params.put("del", DelEnum.NORMAL.getDel());
        return getEntitysCount(params);
    }

    @Override
    public Integer getCountByForm(String corpid, Long formId) {
        Map<String,Object> params = new HashMap<>();
        params.put("corpid",corpid);
        params.put("del", DelEnum.NORMAL.getDel());
        params.put("formId",formId);
        return getEntitysCount(params);
    }

    @Override
    public Integer getEntitysCount(Map<String, Object> param) {
        return shareRuleDao.getEntitysCount(param);
    }

    @Override
    public List findEntitysWithoutSub(Map<String, Object> param) {
        return findEntitys(param);
    }

    @Override
    public List<ShareRuleEntity> findByAuthority(String corpid, List<Long> formIdIn, List<String> roleIdIn, List<String> depIdIn, List<String> userIdIn) {
        return shareRuleDao.findByAuthority(corpid, formIdIn, roleIdIn, depIdIn, userIdIn);
    }
}
